package com.ProdigiousAT;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.ProdigiousAT.DDF;

/**
 * Created by Worker on 01/12/2014.
 */
public class DataDrivenFramework3 {
    int iWait;
    private static WebDriver wd;

    @Before
    public void setUp(){
        wd = new FirefoxDriver();
        //wd = new ChromeDriver();
        iWait = 2000;
    }

    @Test
    public void myMainTest() throws Exception{
        //DDF instance
        DDF dataDrivenFramework = new DDF();

        //Local Variables  to store the TestData
        String siteURL, SearchTerm, SearchResult, vExecute, oSearchResults;


        //Read each set of test data from the excel
        //Declare de XL file path
        String xlPath = "C:/Users/Worker/Desktop/SecondEvaluation.xls";
        // "Sheet1" is the tab name in the excel file
        String[][] myXL = dataDrivenFramework.readXL(xlPath, "Sheet1");

        //System.out.println(myXL.length);
        //See what is received from the XL file
/*
        for (int i=0; i<xRows; i++){
            for(int j=0; j<xCols; j++ ){
                System.out.println(myXL[i][j]+"");
            }
            System.out.println("------------");
        }

*/

        //Take values form eah row in 2D local array "myXL" and load them into local variables
        for(int k=1; k<myXL.length; k++){
            //Execute the Test if "Y"
            vExecute = myXL[k][3];
            if(vExecute.equals("Y")){
                siteURL = myXL[k][0];
                SearchTerm = myXL[k][1];
                SearchResult = myXL[k][2];

                //Execute test
                wd.get(siteURL);
                wd.findElement(By.xpath("//input[@id='masthead-search-term']")).sendKeys(SearchTerm);

                wd.findElement(By.xpath("//button[@id='search-btn']")).click();
                Thread.sleep(iWait);

                oSearchResults = wd.findElement(By.cssSelector(".num-results.first-focus")).getText();
                System.out.println("Your search result is: " + oSearchResults+"\n");

                //Write column "Obtained Output"
                myXL[k][5] = oSearchResults;
                //Validate if the test passed or failed
                if(oSearchResults.equals(SearchResult)) {
                    //Write column Results
                    myXL[k][4] = "Passed!";
                    System.out.print("Test Passed!\n");
                }else {
                    //Write column Results
                    myXL[k][4] = "Failed";
                    System.out.print("Test Failed!\n");
                }
            }else{
                System.out.println("The row: "+ k + " is skipped!");
            }
        }
        dataDrivenFramework.writeXL("C:/Users/Worker/Desktop/Results.xls", "Results2", myXL);
    }

    @AfterClass
    public static void tearDown(){
        wd.close();
        wd.quit();
    }
}
